<?php
	include_once ROOTDIR.'sdks/taobao/TopClient.php';
	include_once ROOTDIR.'sdks/taobao/request/ItemGetRequest.php';
	include_once ROOTDIR.'sdks/taobao/request/ShopGetRequest.php';
	include_once ROOTDIR.'sdks/taobao/request/TaobaokeItemsDetailGetRequest.php';
	
	function fetchurl($url){
		$id = getID($url);
	
		if($id == 0)
			return false;

		$client = new TopClient;
		$client->appkey = 12511402;
		$client->secretKey = '2d1d7389b69b5f95d52a58ee58ab3528';
		//$tao_ke_pid = 29941056;
		
		$req = new ItemGetRequest;
		$req->setFields("detail_url,title,nick,pic_url,price,item_img.url,delist_time,approve_status");
		$req->setNumIid($id);
		$resp = $client->execute($req);
	
		if(!isset($resp->item))
			return false;
		$goods = (array)$resp->item;
	
		return $goods;
	}

	function formatServer($server,$type = 'EN')
	{
		if(empty($server))
			return false;

		if($type == 'DE')
			return unserialize(authcode(base64_decode($server),'DECODE'));
		else
			return base64_encode(authcode(serialize($server),'ENCODE'));
	}
	
	function curl_get_contents($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
		curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$r = curl_exec($ch);
		curl_close($ch);
		return $r;
	}

	function getID($url)
	{
		$id = 0;
		$parse = parse_url($url);
		if(isset($parse['query']))
		{
            parse_str($parse['query'],$params);
			if(isset($params['id']))
				$id = $params['id'];
            elseif(isset($params['item_id']))
                $id = $params['item_id'];
			elseif(isset($params['default_item_id']))
                $id = $params['default_item_id'];	
        }
		return $id;
	}
	
	function getUrlParam($url,$param)
	{
		$re = null;
		$parse = parse_url($url);
		if(isset($parse['query'])){
			parse_str($parse['query'],$params);
			if(isset($params[$param]))
				$re = $params[$param];
		}
		return $re;
	}	

	function getKey($url)
	{
		$id = $this->getID($url);
		return 'taobao_'.$id;
	}
?>